package com.aim.common.service;

import com.aim.common.entity.ZTreeNode;
import com.aim.common.util.JdbcTemplateUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @AUTO 获取ztree节点服务
 * @Author AIM
 * @DATE 2019/4/28
 */
@Service
public class ZtreeNodeService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取部门树节点集合
     */
    public List<ZTreeNode> deptZtree() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT DEPT_ID AS id, PID AS parentId, SIMPLE_NAME AS NAME,");
        sb.append("(CASE WHEN (PID = 0 OR PID IS NULL) THEN 'true' ELSE 'false' END ) AS OPEN ");
        sb.append(" FROM sys_dept");

        List<ZTreeNode> list = JdbcTemplateUtil.queryForListBean(jdbcTemplate, sb.toString(), ZTreeNode.class);
        return list;
    }

    /**
     * 获取角色树节点集合
     */
    public List<ZTreeNode> roleZtree() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ROLE_ID AS id, PID AS parentId, NAME,");
        sb.append("(CASE WHEN (PID = 0 OR PID IS NULL) THEN 'true' ELSE 'false' END ) AS OPEN ");
        sb.append(" FROM sys_role");

        return JdbcTemplateUtil.queryForListBean(jdbcTemplate, sb.toString(), ZTreeNode.class);
    }

    /**
     * 获取栏目树节点集合
     */
    public List<ZTreeNode> menuZtree() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT sm.`MENU_ID` AS id, IFNULL(ms.`MENU_ID`,0) AS parentId, sm.`NAME`, sm.`CODE`, ");
        sb.append("(CASE WHEN (sm.`PCODE` = 0 OR sm.`PCODE` IS NULL) THEN 'true' ELSE 'false' END ) AS OPEN, ");
        sb.append("sm.`LEVELS`  FROM `sys_menu` sm LEFT JOIN `sys_menu` ms ON sm.`PCODE` = ms.`CODE`");

        return JdbcTemplateUtil.queryForListBean(jdbcTemplate, sb.toString(), ZTreeNode.class);
    }

    public List<ZTreeNode> dictZtree() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT `DICT_ID` AS id,`PID` AS parentId,`NAME`,");
        sb.append("'false' AS OPEN ");
        sb.append(" FROM `sys_dict`");

        return JdbcTemplateUtil.queryForListBean(jdbcTemplate, sb.toString(), ZTreeNode.class);
    }
}
